﻿<?php
// ------------------------------------------------------------------
// NEEDED STUBS FOR THIS EXAMPLE
// ------------------------------------------------------------------


// this will include all needed files to be able to build a Request
include_once($CONF["STUBS_FOLDER"]."Map24MatrixRouting/Map24MatrixRouting.php5");


// ------------------------------------------------------------------
// NEEDED PARAMETERS FOR THIS REQUEST
// ------------------------------------------------------------------


// definition of the Points

// first Point
$pIdx=0;
$Points[$pIdx]['Longitude']='800.04816';
$Points[$pIdx]['Latitude']='3151.0925';

// another Point
$pIdx++;
$Points[$pIdx]['Longitude']='800.04816';
$Points[$pIdx]['Latitude']='3149.0925';

// another Point
$pIdx++;
$Points[$pIdx]['Longitude']='798.04816';
$Points[$pIdx]['Latitude']='3149.0925';

// another Point
$pIdx++;
$Points[$pIdx]['Longitude']='798.04816';
$Points[$pIdx]['Latitude']='3151.0925';

// another Point
$pIdx++;
$Points[$pIdx]['Longitude']='799.04816';
$Points[$pIdx]['Latitude']='3150.0925';


// definition of the Properties

// a Property
$proIdx=0;
$property[$proIdx]=new Property();
$property[$proIdx]->Key="REPLY_NODES";
$property[$proIdx]->Value="FALSE";

// a Property
$proIdx++;
$property[$proIdx]=new Property();
$property[$proIdx]->Key="REPLY_DISTANCES";
$property[$proIdx]->Value="FALSE";

// a Property
$proIdx++;
$property[$proIdx]=new Property();
$property[$proIdx]->Key="REPLY_TRAVELTIMES";
$property[$proIdx]->Value="TRUE";
$proIdx++;

//show routes
$SynchronizeClient = "TRUE";


// ------------------------------------------------------------------
// BUILDING THE REQUEST
// ------------------------------------------------------------------


// definition of the request header with Map24ID and SessionID
$header = new RequestHeader();
$header->Map24ID  = $CONF['MAP24_ID'];
$header->ClientID = $CONF['SESSION_ID'];

// definition of the request body
$request = new CalculateMRoutesMatrixRequest();

//define the Points Addresses by Coordinates
for($i=0;$i<count($Points);$i++)
{
	$PointsAry[$i]= new MRoutePoint();
	$PointsAry[$i]->Coordinate= new Coordinate();
	$PointsAry[$i]->Coordinate->Longitude = $Points[$i]['Longitude'];
	$PointsAry[$i]->Coordinate->Latitude = $Points[$i]['Latitude'];
}
if(count($PointsAry)>0)	{ $request->Points=$PointsAry; }

//Show Routes
$request->SynchronizeClient= $SynchronizeClient;


// define the Properties
if(is_array($property)) { $request->Properties = $property; }

// ------------------------------------------------------------------
// EXECUTE THE REQUEST
// ------------------------------------------------------------------


// in this object the SoapClient will be initiated and executed
$routing = new Map24MatrixRouting($CONF['MAPTP_SYSTEM'],$CONF['MAP24_ID'],$CONF['SESSION_ID']);
$response = $routing->calculateMRoutesMatrix($header,$request);


// ------------------------------------------------------------------
// OUTPUT THE RESULT
// ------------------------------------------------------------------


$resultString = "";
$counter = 0;

if( isset($response->RouteVectors) )
{
	foreach ($response->RouteVectors as $result)
	{
		$counter++;
		$resultString .= "<div style='border:1px solid black;margin:2px 0px;padding:5px;'>".$counter."<br>";
		foreach ($result->Routes as $Route) {
			$resultString .= "MRouteID: " . $Route->MRouteID . "<br /><br />";
			$resultString .= "TotalDistance: " . $Route->TotalDistance . " meters<br /><br />";
		}
		$resultString .= "</div>";

		//We want to save the MRouteID of the second route in the session for a later example
		if($counter==2) { $RouteID=$Route->MRouteID; }
	}
	// save the MRouteID in a cookie (needed to show the route later)
	$t=date("U")+3600;
	SetCookie("MRouteID",$RouteID,$t);
}
else
{
	$resultString = "Sorry, no answer from the webservices";
}

?>